<?php
class ZMAction extends Action
{
    /**
    * 获取应用的路径
    * 
    */
    protected function getAppPath(){
        $app_url = dirname(__APP__);
        $app_url = str_replace("\\","/",$app_url);
        if(substr($app_url, -1, 1)!='/') $app_url .= '/';
        return $app_url;
    }
    
    //检查权限
    protected function checkPriv($action){
        $user = $_SESSION['user'];
        $userAct = A('User');
        return $userAct->checkPriv($action);
    }
    
    protected function ajaxRetArray($result,$count=null){
        if($result==null){
            $result = array();
        }
        if($count==null){
            $count = count($result);
        }
        $this->ajaxReturn($result,$count,true);
    }
    
    /**
    * 获取属性值
    * 
    * @param mixed $vtid
    * @param mixed $val
    */
    protected function getValue($vtid,$val){
        $TypeDef = M('ValTypeDef');
        $resType = $TypeDef->where("vtid=$vtid")->find();
        if($resType){
            $ModelVal = M($resType['valtable']);
            if(is_numeric($val)){
                $value = $ModelVal->where("id=$val")->find(); 
                $resType['value'] = $value['val'];
            }else{
                $resType['value'] = '';
            }
            return $resType;
        }
        return false;
    }
    
    /**
    * 删除属性值
    * 
    * @param mixed $vtid
    * @param mixed $id
    */
    protected function delValue($vtid,$id,$devid=0){
        $TypeDef = M('ValTypeDef');
        $resType = $TypeDef->where("vtid=$vtid")->find();
        if($resType){
            $ModelVal = M($resType['valtable']);
            if(is_numeric($id)){
                $ModelVal->where("id=$id")->delete(); 
            }
        }
        if(!empty($devid)){
            $actDev = A('Dev');
            $actDev->delDev($devid);
        }
    }
    
    /**
    * 添加值
    * 
    * @param mixed $vtid
    * @param mixed $value
    */
    protected function addValue($vtid,$value){
        $TypeDef = M('ValTypeDef');
        $resType = $TypeDef->where("vtid=$vtid")->find();
        if($resType){
            $ModelVal = M($resType['valtable']);
            $data['val'] = $value;
            $valid = $ModelVal->add($data);
            return $valid;
        }
        return false;
    }
    
//    protected function getAttrDef($vtid,$val){
//        $TypeDef = M('ValTypeDef');
//        $resType = $TypeDef->where("vtid=$vtid")->find();
//        if($resType){
//            if(is_numeric($val)){
//                $ModelVal = M($resType['valtable']);
//                $value = $ModelVal->where("id=$val")->find(); 
//                $resType['value'] = $value;
//            }
//            return $resType;
//        }
//        return false;
//    }
    
    /**
    * 获取属性值
    * 
    * @param mixed $attrid  属性ID
    * @param mixed $val     属性值
    */
    protected function getAttrVal($attrid,$val){
        $AttrDef = M('DevAttrDef');
        $TypeDef = M('ValTypeDef');
        $resAttr = $AttrDef->where("attrid=$attrid")->find();       
        if($resAttr){
            $vtid = $resAttr['vtid'];//获取属性类别
            $resType = $TypeDef->where("vtid=$vtid")->find();
            if($resType){
                $ModelVal = M($resType['valtable']);
                if(is_numeric($val)){
                    $value = $ModelVal->where("id=$val")->find(); 
                    $resAttr['editor'] = $resType['editor'];
                    
                    if($resType['editor']=='datefield'){//日期格式化
                        $date = date_create($value['val']);
                        $resAttr['value'] = date_format($date, 'Y-m-d');
                    }else if($resType['editor']=='timefield'){ //时间格式化
                        $date = date_create($value['val']);
                        $resAttr['value'] = date_format($date, 'H:i:s');
                    }else{
                        $resAttr['value'] = $value['val'];                        
                    }                    
                    return $resAttr;
                }
            }
        }
        return false;
    }
    
    protected function getDevAttrVal($devid,$attrid){
        $DevAttr = M('DevAttr');
        $map['devid'] =  $devid;
        $map['attrid'] =  $attrid;
        $resDevAttr = $DevAttr->where($map)->find();
        if($resDevAttr){
            $val = $resDevAttr['val'];
            $result = $this->getAttrVal($attrid,$val);
            if($result!==false){
                $result['isvisible'] = $resDevAttr['isvisible'];
                $result['vval'] = $resDevAttr['val'];
            }
            return $result;
        }
        return false;
    }
    
    /**
    * 根据属性id和属性id的值来设置属性
    * 
    * @param mixed $attrid 属性ID
    * @param mixed $val    
    * @param mixed $value
    */
    protected function setAttrVal($attrid,$val,$value){
        $AttrDef = M('DevAttrDef');
        $TypeDef = M('ValTypeDef');
        $resAttr = $AttrDef->where("attrid=$attrid")->find();       
        if($resAttr){
            $vtid = $resAttr['vtid'];//获取属性类别
            $resType = $TypeDef->where("vtid=$vtid")->find();
            if($resType){
                if($resType['editor']=='data'){
                    return $value;
                }else{
                    $ModelVal = M($resType['valtable']);
                    if(is_numeric($val)){
                        $data['val'] = $value;
                        $ModelVal->where("id=$val")->save($data); 
                    }else{
                        $data['val'] = $value;
                        $val = $ModelVal->add($data);
                    }
                }
                return $val;
            }
        }
        return false;
    }                            
    
    /**
    * 设置设备属性值
    * 
    * @param mixed $devid 设备id
    * @param mixed $attrid 属性id
    * @param mixed $value 属性值
    */
    protected function setDevAttrValue($devid,$attrid,$value,$isvisible=null){
        if($isvisible!==null){
            $data['isvisible'] = empty($isvisible)?0:1;
        }
        $DevAttr = M('DevAttr');
        $map['devid'] =  $devid;
        $map['attrid'] =  $attrid;
        $resDevAttr = $DevAttr->where($map)->find();
        if($resDevAttr){
            $val = $resDevAttr['val'];
            $val = $this->setAttrVal($attrid,$val,$value);
            if($val !== false){
                $data['val'] = $val;
                $DevAttr->where($map)->save($data);
            }
        }else{
            $val = $this->setAttrVal($attrid,$val,$value);
            if($val !== false){
                $data['devid'] = $devid;
                $data['attrid'] =  $attrid;
                $data['val'] = $val;
                $DevAttr->add($data);
            }
        }        
    }
    
    protected function setDevXYZ($devid,$x,$y,$z){
        //获取空间坐标
        $model = M('Dev');
        $map['parentid'] = $devid;
        $map['istemp'] = 0;
        $map['ddid'] = C('XYZ');//定死了？空间坐标类型
        $resXYZ = $model->where($map)->find();
        if($resXYZ){
            $xyzDev = $resXYZ['devid'];
            $this->setDevAttrValue($xyzDev,C('X'),$x);
            $this->setDevAttrValue($xyzDev,C('Y'),$y);
            $this->setDevAttrValue($xyzDev,C('Z'),$z);
        }        
    }
    
    /**
    * 获取设备变换的参数
    * 
    * @param mixed $devid  设备ID
    */
    protected function getDevTransform($devid){
        $actDev = A('Dev');
        $model = M('Dev');
        
        //获取空间坐标
        $map['parentid'] = $devid;
        $map['istemp'] = 0;
        $map['ddid'] = C('XYZ');//定死了？空间坐标类型
        $resXYZ = $model->where($map)->find();
        if($resXYZ){
            $ret = array();
            $rotation = '';
            $center = '';
            $scale = '';
            $x = 0;
            $y = 0;
            $z = 0;
            $resAttr = $actDev->getDevAttr($resXYZ['devid']);
            if($resAttr){
                foreach($resAttr as $row){
                    if($row['attrname']=='X'){
                        if(!empty($row['defval'])) $x = $row['defval'];
                    }else if($row['attrname']=='Y'){
                        if(!empty($row['defval'])) $y = $row['defval'];
                    }else if($row['attrname']=='Z'){
                        if(!empty($row['defval'])) $z = $row['defval'];
                    }else if($row['attrname']=='旋转'){
                        $rotation = $row['defval'];
                    }else if($row['attrname']=='缩放'){
                        $scale = $row['defval'];
                    }else if($row['attrname']=='操作基点'){
                        $center = $row['defval'];
                    }
                }
            }
            $ret['devid'] = $devid;
            $ret['translation'] = "$x $y $z";
            $ret['rotation'] = $rotation;
            $ret['center'] = $center;
            $ret['scale'] = $scale;
            return $ret;
        }
        return null;
    }
    
    /**
    * 获取模型URL
    * 
    * @param mixed $ddid
    */
    protected function getModelUrl($ddid){
        $model = M('DevDef');
        $res = $model->where("ddid=$ddid")->find();
        if($res){
            return $this->getAppPath() . C('MODELURL') . $res['modelurl'];
        }
        return '';
    }
    
    /**
    * 根据属性名称获取属性值
    * 
    * @param mixed $devid   设备ID
    * @param mixed $attrname  属性名称
    */
    public function getAttrByName($devid,$attrname){
        $ModelDev = M('Dev');
        $resDev = $ModelDev->where("devid=$devid")->find();
        if($resDev){
            $ddid = $resDev['ddid'];
            $where['ddid'] = $ddid;
            $where['attrname'] = $attrname;
            $ModelAttrDef = M('DevAttrDef');
            $resAttrDef = $ModelAttrDef->where($where)->find();
            if($resAttrDef){
                return $this->getDevAttrVal($devid,$resAttrDef['attrid']);
            }
        }
        return false;
    }
    
    protected function ajaxReturnWithMeta($data,$msg,$success,$metadata=null) {
        $info['data']   =   $data;
        $info['msg']=   $msg;
        $info['success'] =  $success;
        if($metadata) $info['metaData'] = $metadata;
        $data           =   $info;
        // 返回JSON数据格式到客户端 包含状态信息
        header('Content-Type:application/json; charset=utf-8');
        exit(json_encode($data));
    }
    
}
?>